<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    {include file="../tpl/header.html" /}
</head>
<body>
{include file="../tpl/menu.html" /}
<div class="layui-fluid">
    <div class="layui-row">
        <form action="{:url('admin/admin/addRule')}" method="post" class="layui-form layui-form-pane">

            <div class="layui-form-item layui-form-text">
                {volist name="list" id="vo"}
                <div>
                <label class="layui-form-label" style="height: 50px;">
                    <input type="checkbox" {if condition="in_array($vo['id'],$list_auth)"}checked="checked"{/if} name="pid[]" value="{$vo.id}" lay-skin="primary" lay-filter="father" title="{$vo['name']}">
                </label>
                <table  class="layui-table layui-input-block">
                    <tbody>
                    {php}$list1 = $vo['children'];{/php}
                    {volist name="list1" id="vo1"}
                    <tr>
                        <td style="width: 80px;">
                            <input type="checkbox" {if condition="in_array($vo1['id'],$list_auth)"}checked="checked"{/if} name="pid[]" value="{$vo1.id}" lay-skin="primary" lay-filter="father1" title="{$vo1['name']}">
                        </td>
                        <td>
                            <div class="layui-input-block">
                                {php}$list2 = $vo1['children'];{/php}
                                {volist name="list2" id="vo2"}
                                <input name="id[]" {if condition="in_array($vo2['id'],$list_auth)"}checked="checked"{/if} lay-skin="primary" type="checkbox" value="{$vo2.id}" lay-filter="father2" title="{$vo2.name}">
                                {/volist}
                            </div>
                        </td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>
                </div>
                {/volist}
            </div>
            <input type="hidden" name="role_id" value="{$data.role_id}">
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">更新</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;
        //监听提交
        form.on('submit(add)', function(data){
            $.ajax({
                type:'post',
                data:data.field,
                dataType:'json',
                url:"{:url('admin/admin/addRule')}",
                success:function (e) {
                    if(e.code === 1){
                        layer.msg(e.msg, {icon: 6,time: layer_msg_success_time},
                            function() {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.location.reload();
                            });
                    }else{
                        layer.msg(e.msg, {icon: 5,time: layer_msg_error_time});
                    }
                }
            });
            //发异步，把数据提交给php
            return false;
        });

        form.on('checkbox(father)', function(data){
            if(data.elem.checked){
                $(data.elem).parent('.layui-form-label').siblings('table').find('input').prop("checked", true);
                form.render();
                $(data.elem).prop("checked", false);
            }else{
                $(data.elem).parent('.layui-form-label').siblings('table').find('input').prop("checked", false);
                form.render();
                $(data.elem).prop("checked", true);
            }
        });

        form.on('checkbox(father1)', function(data){
            if(data.elem.checked){
                $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                $(data.elem).parents('.layui-table').siblings('.layui-form-label').find('input').prop("checked", true);
                form.render();
            }else{
                $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                //$(data.elem).parents('.layui-table').siblings('.layui-form-label').find('input').prop("checked", false);
                form.render();
            }
        });

        form.on('checkbox(father2)', function(data){
            if(data.elem.checked){
                $(data.elem).parents('td').siblings('td').find('input').prop("checked", true);
                $(data.elem).parents('.layui-table').siblings('.layui-form-label').find('input').prop("checked", true);
                form.render();
            }else{
                // $(data.elem).parents('td').siblings('td').find('input').prop("checked", false);
                // $(data.elem).parents('.layui-table').siblings('.layui-form-label').find('input').prop("checked", false);
                // form.render();
            }
        });

    });
</script>
</body>
</html>